import {
  _path,
  existplayer,
  bound,
  Write_cangku,
  _item
} from '../../mode/allapi'
import { plugin ,AMessage} from '../../api/api'
export class upgrade extends plugin {
  constructor() {
    super({
      dsc: '晋升',
      rule: [
        {
          reg: /^(#|\/)?晋升.*$/,
          fnc: 'jinsheng'
        }
      ]
    })
  }

  async jinsheng(e:AMessage) {
    const id = e.user_id
    if (!(await existplayer(id))) return false
    const expecItem = e.msg.replace(/(#|\/)?晋升/, '')
    const cangku = await (await _path(id)).cangku
    const foundHun = await bound('将魂', cangku, 'name')
    const bingyin = await (await _path(id)).bingyin
    const foundJiang = await bound(expecItem, bingyin, 'name')
    if (!foundJiang) return e.reply(`您没有【${expecItem}】,或此角色已上阵`)
    const kill = 200 * (1 + 0.2 * foundJiang.collection)
    if (foundHun && foundHun.num >= kill) {
      if (foundJiang.collection < 5) {
        foundHun.num -= kill
        const level = foundJiang.level
        const upatk = 0.1 * foundJiang.increase_atk
        const upiq = 0.1 * foundJiang.increase_iq
        const updef = 0.1 * foundJiang.increase_def
        foundJiang.atk = Math.round(((level - 1) * upatk + foundJiang.atk)*100)/100
        foundJiang.iq = Math.round(((level - 1) * upiq + foundJiang.iq)*100)/100
        foundJiang.def = Math.round(((level - 1) * updef + foundJiang.def)*100)/100
        foundJiang.increase_atk = Math.round((upatk+foundJiang.increase_atk)*100)/100
        foundJiang.increase_iq = Math.round((upiq+foundJiang.increase_iq)*100)/100
        foundJiang.increase_def = Math.round((updef+foundJiang.increase_def)*100)/100
        foundJiang.collection += 1
      } else {
        return e.reply(`【${expecItem}】已达到最大晋升，不能再做晋升了`)
      }
    } else {
      const shortage = kill - foundHun.num
      return e.reply(`您的将魂不足，还需要${shortage}个将魂`)
    }
    await Write_cangku('cangku', id, cangku)
    await Write_cangku('barracks', id, bingyin)
    e.reply(
      `消耗将魂*${kill},成功将【${expecItem}】提升至【${foundJiang.collection}星】`
    )
    return true
  }
}
